package com.omicrondev.flightops;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class EditCgStation extends Activity {
    EditText nameBox, armBox, minWeightBox, maxWeightBox;
    CheckBox fuelCheck, hiddenCheck;
    FlightOps mApp;
    CGStation mStation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_cg_station);

        mApp = (FlightOps)getApplicationContext();

        nameBox = (EditText)findViewById(R.id.name);
        armBox = (EditText)findViewById(R.id.arm);
        minWeightBox = (EditText)findViewById(R.id.minWeight);
        maxWeightBox = (EditText)findViewById(R.id.maxWeight);
        fuelCheck = (CheckBox)findViewById(R.id.fuel);
        hiddenCheck = (CheckBox)findViewById(R.id.hidden);

        Intent intent = getIntent();

        int index = intent.getIntExtra("station", 0);

        mStation = mApp.mAcType.mStations.get(index);

        nameBox.setText(mStation.mName);
        DecimalFormat df = new DecimalFormat("#.##");
        armBox.setText(df.format(mStation.mArm));
        minWeightBox.setText(((Integer) mStation.mMinWeight).toString());
        maxWeightBox.setText(((Integer)mStation.mMaxWeight).toString());
        fuelCheck.setChecked(mStation.mIsFuel);
        hiddenCheck.setChecked(mStation.mIsHidden);

        nameBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                EditText input = (EditText)view;
                if (!hasFocus) {
                    mStation.mName = ((EditText) view).getText().toString();
                }
            }
        });

        armBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!hasFocus) {
                    BigDecimal value;
                    try {
                        value = new BigDecimal(((EditText) view).getText().toString());
                    } catch (NumberFormatException e) {
                        value = BigDecimal.ZERO;
                    }

                    mStation.mArm = value.doubleValue();
                }
            }
        });

        minWeightBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!hasFocus) {
                    BigDecimal value;
                    try {
                        value = new BigDecimal(((EditText) view).getText().toString());
                    } catch (NumberFormatException e) {
                        value = BigDecimal.ZERO;
                    }

                    mStation.mMinWeight = value.intValue();
                }
            }
        });

        maxWeightBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!hasFocus) {
                    BigDecimal value;
                    try {
                        value = new BigDecimal(((EditText)view).getText().toString());
                    } catch (NumberFormatException e) {
                        value = BigDecimal.ZERO;
                    }

                    mStation.mMaxWeight = value.intValue();
                }
            }
        });

        fuelCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mStation.mIsFuel = b;
            }
        });

        hiddenCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mStation.mIsHidden = b;
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.edit_cg_station, menu);
        return true;
    }
    
}
